package club.ljwang.service;

import club.ljwang.constant.MessageConstant;
import club.ljwang.dao.SetmealMapper;
import club.ljwang.entity.PageResult;
import club.ljwang.entity.QueryPageBean;
import club.ljwang.entity.Result;
import club.ljwang.pojo.CheckGroup;
import club.ljwang.pojo.CheckItem;
import club.ljwang.pojo.Setmeal;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Auther: wangl
 * @Date: 2020/2/27 21:58
 * @Description:
 */
@Service(interfaceClass = SetmealService.class )
@Transactional
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public Result add(Setmeal setmeal) {

        setmealMapper.add(setmeal);

        setmealMapper.insertSetmealCheckgroups(setmeal.getCheckgroupIds(),setmeal.getId());
        return new Result(true, MessageConstant.ADD_SETMEAL_SUCCESS);
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        //1.告诉Mybatis我们要分页
        Page page = PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        //2.写一个跟分页毫无关系的sql
        List<Setmeal>  setmeals = setmealMapper.findPage(queryPageBean.getQueryString());
        return new PageResult(page.getTotal(),setmeals);
    }

    @Override
    public List<Setmeal> getSetMeal() {

        return setmealMapper.getSetMeal();
    }

    @Override
    public Setmeal findById(Integer id) {
        //1.通过id查询套餐
        Setmeal setmeal = setmealMapper.findById(id);

        if (setmeal != null) {
            //2.通过套餐id去关系表查询检查组
            List<CheckGroup> checkGroups = setmealMapper.findCheckGroupsBySetmealId(id);

            for (CheckGroup checkGroup : checkGroups) {

                List<CheckItem> checkItems = setmealMapper.findCheckItemsByCheckGroupsId(checkGroup.getId());
                checkGroup.setCheckItems(checkItems);

            }

            setmeal.setCheckGroups(checkGroups);

        }
        return setmeal;
    }
}
